

package tomkitty;

public class TkTurdletFactory extends Turdlet implements TurdletFactory {

  public void doit() {

    try {
      String path = request.getUri().getPath();

      String[] segments = path.split("/", 4);

      if (segments.length < 3) {
        throw new TkResponseException(HTTP_NOT_FOUND);
      }

      Class c = context.getClass(segments[2]);

      if (c == null) {
        throw new TkResponseException(HTTP_NOT_FOUND);
      }

      Handler h = (Handler) c.newInstance();
      h.init(handler, context).handle(request, response);

      request = null;
      response = null;
    } catch (TkResponseException e) {
      response.setStatus(e.getMessage());
    } catch (Exception ee) {
      response.setStatus(HTTP_SERVER_ERROR);
    }

  }

}
